Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
Javascript Regex to convert _xxx_ to [i]xxx[i] ignoring any text between <> (URLs) or `` (code)

I'm trying to reformat Slack formatting to bbcode and need a little help. Slack does italics like this:

_this is italic_ and this isn't

My current expression (/\_([^\_]*)\_/gm) works but unfortunately picks up underscores in URLs and inside code snippets. Slack formats URLs and code like this:

<www.thislink.com|here's a link>
`here's a code snippet`

How can I tell regex not to match any underscore pairs inside a link or code snippet? I've been trying negative lookahead and lookbehind but without success.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to match and capture what you need and just match what you do not need.

Once you get a match, analyze it and implement the appropriate code logic:

const re = /<[^<>|]*(?:\|[^<>]*)?>|`[^`]*`|_([^_]*)_/g;
const text = "<www.thislink.com|here's a link>\n`here's a code snippet`\n_this is italic_ and this isn't";
console.log( text.replace(re, (m,g) => g !== undefined ? "[i]" + g + "[/i]" : m ) )

See the regex demo. Details:

  • <[^<>|]*(?:\|[^<>]*)?> - a <, then zero or more chars other than <, > and |, then an optional sequence of a | and then zero or more chars other than < and > and then a > char
  • | - or
  • `[^`]*` - a backtick, zero or more chars other than a backtick and a backtick
  • | - or
  • _([^_]*)_ - _, Group 1: zero or more chars other than _, a _.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda